clear all
set more off, perm
local mydir "`1'"
global Tables	`mydir'/XS/Tables
global Work	`mydir'/XS/DataWork
global Raw	`mydir'/XS/DataRaw
global WRDS	`mydir'/WRDS
* *******************************************
set varabbrev off
* *******************************************
* TimeSeries01.dta created by TimeSeriesForXS.do
use $Work/TimeSeries01, clear

* FF01.dta created by FF01.do 
merge 1:1 datem using $Work/FF01
keep if _merge==3
drop _merge 
* *******************************************
* remove "*" from next line
*save $Data/Table7, replace
* *******************************************
* Prepare data on Forecast Errors for FF portfolios
* *******************************************
* FamaFrench05bb.dta generated by FamaFrench05bb.do
use if (LTG+bookeq<.) & (bookeq>0) using $Work/FamaFrench05bb, clear
* add returns (data generated by FamaFrench02.do)
merge 1:1 permco permno datem using $Work/FamaFrench02Ret, keepusing (permco permno datem mcap0-mcap60 ret1-ret60)
keep if _merge==3
drop _merge
* add earnings (earnings.dta by earnings.do)
merge 1:1 permco permno datem using $Work/earnings
drop if _merge==2
drop _merge
keep if SIZE>=1 & SIZE<=10
* size portfolios will be based on above/below median  
quietly: generate B=ceil(SIZE/5)
drop SIZE
* *******************************************
tempfile tmp
save "`tmp'", replace
* *******************************************
* do calculations for Fama French portfolios (will work with 3x2 portfolios)
foreach RNK in BOOK PROF INVE { 
use if `RNK'<. using "`tmp'", clear
quietly: generate H=1 if `RNK'>=01 & `RNK'<=03
quietly: replace  H=3 if `RNK'>=04 & `RNK'<=07
quietly: replace  H=5 if `RNK'>=08 & `RNK'<=10
drop `RNK'
save $Temp/tmp`RNK', replace
* *********************************
* Compute portfolio-level variables for portfolio that is rebalanced monthly
preserve
forvalues x=1(1)60 {
	local j=`x'-1
	bys H B datem: egen TM=total(mcap`j' * (!missing(ret`x')))  
	bys H B datem: gen vwret`x'=ret`x'*(mcap`j'/TM)
	drop TM
	quietly: gen EP`x'=F`x'street if mcap`x'<.
}
collapse (sum) mcap* vwret* EP*, by(H B datem)
forvalues x=1(1)60 {
	quietly: replace  mcap`x'=. if  mcap`x'==0
	quietly: replace vwret`x'=. if vwret`x'==0
	quietly: replace    EP`x'=. if    EP`x'==0
}
gen p=10*H+B
tsset p datem
drop p

save $Temp/portf, replace
restore
/* ********************************* */
keep permco permno datem H B streetLTM*
*#2) Work with firms that get delisted 
*#2.a) add data on returns, earnings and capitalization of FF portfolios
merge m:1 H B datem using $Temp/portf
drop _merge

*#2.b) find data on distributions when delisted (data generated by Delistings02.do)
merge m:1 permco permno using $Work/Delistings02
keep if _merge==3
drop _merge
order permco permno datem dlstmo H B dlstcd distrib 

*#2.c) WORK with relevant obs 
* Firm is delisted --> investor earns return on rebalanced portfolio --> original investment is assigned fractional earnings of rebalanced portfolio.
gen T=dlstmo-datem
keep if T>0 & T<61

su T, d
gen capital0=.
forvalues f=1(1)60 {
	local i=`f'-1
	quietly: generate capital`f'=distrib if `f'==T
	quietly: replace  capital`f'=capital`i'*(1+vwret`f') if `f'>T
	quietly: generate  E`f'=(capital`f'/mcap`f')*EP`f'
}
* keep data on earnings in yearly intervals
forvalues x=1(1)5 {
	local t=12*`x'
	quietly: gen REINV`x'=E`t'
}
keep permco permno datem dlstmo  REINV1 REINV2 REINV3 REINV4 REINV5
order permco permno datem dlstmo REINV1 REINV2 REINV3 REINV4 REINV5

merge 1:1 permco permno datem using $Temp/tmp`RNK'
drop if _merge==1
drop _merge
* ********** 
* forecasts (use medest_t when available; else proxy medest_t based on LTG and medest_t-1)
generate f1=medest1*shs0/1000	if nsecurities==1 
generate f2=medest2*shs0/1000	if nsecurities==1	 
generate f3=medest3*shs0/1000	if nsecurities==1
generate f4=medest4*shs0/1000	if nsecurities==1	 
generate f5=medest5*shs0/1000	if nsecurities==1	 

replace f2=(1+LTG/100)*f1 if LTG>-100 & f1>0 & f2==.
replace f3=(1+LTG/100)*f2 if LTG>-100 & f2>0 & f3==.
replace f4=(1+LTG/100)*f3 if LTG>-100 & f3>0 & f4==.
replace f5=(1+LTG/100)*f4 if LTG>-100 & f4>0 & f5==.

* earnings 
generate e1=streetLTM1*(shs0/shs1)
generate e2=streetLTM2*(shs0/shs2)
generate e3=streetLTM3*(shs0/shs3)
generate e4=streetLTM4*(shs0/shs4)
generate e5=streetLTM5*(shs0/shs5)
* when firm is delisted (dlstmo>=datem), assing earnins from rebalanced portfolio
forvalues x=1(2)5 {
	generate new_e`x'=e`x'
	replace  new_e`x'=REINV`x'	if e`x'==. & (dlstmo>=datem)
	drop e`x'
}
tsset permco datem
generate nume1=new_e1 		if f1+new_e1<.
generate base1=f1 	 	if f1+new_e1<.

generate nume3=new_e3	 	if LTG+new_e3+L3.streetLTM<.
generate base3=L3.streetLTM 	if LTG+new_e3+L3.streetLTM<.

generate nume5=new_e5 		if LTG+new_e5+L3.streetLTM<.
generate base5=L3.streetLTM 	if LTG+new_e5+L3.streetLTM<.

gen ege1=ln(nume1)/1-ln(base1)/1
gen ege3=ln(nume3)/3-ln(base3)/3 - LTG/100
gen ege5=ln(nume5)/5-ln(base5)/5 - LTG/100

foreach x of varlist ege1 ege3 ege5 {
	quietly: bys datem: egen max=pctile(`x'), p(99)
	quietly: bys datem: egen min=pctile(`x'), p(1)  
	quietly: replace `x'=max if `x'>max & `x'<.
	quietly: replace `x'=min if `x'<min	
	drop max min
}	
collapse (mean) ege1 ege3 ege5, by(datem H B)
foreach Z in ege1 ege3 ege5  {
	replace `Z'=. if `Z'==0
	}

keep if dofm(datem)<mdy(1,1,2016)
foreach x of varlist ege1 ege3 ege5  {
	quietly: bys H B: egen max=pctile(`x'), p(99)
	quietly: bys H B: egen min=pctile(`x'), p(1)  
	quietly: replace `x'=max if `x'>max & `x'<.
	quietly: replace `x'=min if `x'<min	
	drop max min 
}
* remove "*" from next line
*save $Data/errors_`RNK', replace
}
***************************************** 
* Prepare data on FEs for LTG portfolio
***************************************** 
* XSection04.dta generated by XSection04.do 
use datem RNK ege1 ege3 ege5 if dofm(datem)<mdy(1,1,2016) using $Work/XSection04, clear
reshape wide ege1 ege3 ege5, j(RNK) i(datem)

* TimeSeries01.dta created by TimeSeriesForXS.do
merge m:1 datem using $Work/TimeSeries01, keepusing(datem LTG) 
keep if _merge==3 & LTG<.
drop _merge

* High minus low
gen LMH5=ege51-ege510
gen LMH3=ege31-ege310

keep if dofm(datem)<mdy(1,1,2016)
foreach var of varlist  ege1* ege3* ege5* LMH3 LMH5 LTG  {
	egen tmp =std(`var')
	quietly: replace `var'=tmp
	drop tmp
}
tsset datem
gen L1LTG=L12.LTG
gen d1LTG=LTG-L1LTG
keep if d1LTG<.
**************************
* remove "*" from next line
* save $Data/Table8, replace
!rd $Temp/*.dta -f
**************************
